package mainWindowSupport;

import java.util.ArrayList;
import java.util.List;

import javax.swing.JTable;

import cuShapes.PQShape;

public class ShapePropTable {

	JTable currentTable;
	JTable shapeTable;
	JTable arrowTable;
	
	public ShapePropTable() {
			
		shapeTable = new JTable();
		arrowTable = new JTable();
		currentTable = shapeTable;
		
		
		List<String> rows = new ArrayList<String>();
		rows.add("Text");
		rows.add("X");
		rows.add("Y");
		rows.add("Width");
		rows.add("Height");
		rows.add("Color");
		initComponents(shapeTable, rows);

		rows.clear();
		rows.add("Text");
		rows.add("X");
		rows.add("Y");
		rows.add("Length");
		rows.add("Color");
		initComponents(arrowTable, rows);
	}
	
	@SuppressWarnings("serial")
	private void initComponents(JTable table, List<String> numRows) {

        table.setModel(new javax.swing.table.DefaultTableModel(
            new Object [numRows.size()][2],
            new String [] {"General", ""}
        ) {
            Class<?>[] types = new Class<?>[] {
                java.lang.String.class, java.lang.String.class
            };
            boolean[] canEdit = new boolean [] {
                false, true
            };

            public Class<?> getColumnClass(int columnIndex) {
                return types [columnIndex];
            }

            public boolean isCellEditable(int rowIndex, int columnIndex) {
                return canEdit [columnIndex];
            }
        });
        for(int i=0;i<numRows.size();i++)
        	table.getModel().setValueAt(numRows.get(i), i, 0);
    }
	
	public JTable getCurrentTable() {
		return currentTable;
	}
	public JTable getShapeTable() {
		return shapeTable;
	}
	public JTable getArrowTable() {
		return arrowTable;
	}
	
	public void setProperties(PQShape shape) {
		double width = Math.abs(Math.abs(shape.getStart().getX()) - Math.abs(shape.getEnd().getX()));
		double height = Math.abs(Math.abs(shape.getStart().getY()) - Math.abs(shape.getEnd().getY()));
		double length = 0;
		
		if(shape.getShapeClass().equals("Arrow")) {
			length = Math.sqrt(width*width + height*height);
			currentTable = arrowTable;
			currentTable.getModel().setValueAt(String.valueOf(length), 3, 1);
			currentTable.getModel().setValueAt(shape.getColor(), 4, 1);
		} else {
			currentTable = shapeTable;
			currentTable.getModel().setValueAt(String.valueOf(width), 3, 1);
			currentTable.getModel().setValueAt(String.valueOf(height), 4, 1);
			currentTable.getModel().setValueAt(shape.getColor(), 5, 1);
			//shapeTable.updateUI();
		}
		
		currentTable.getModel().setValueAt(shape.getName(), 0, 1);
		currentTable.getModel().setValueAt(String.valueOf(shape.getStart().getX()), 1, 1);
		currentTable.getModel().setValueAt(String.valueOf(shape.getStart().getY()), 2, 1);
	}
	
	public void clear() {
		for(int i=0; i<currentTable.getModel().getRowCount();i++)
			currentTable.getModel().setValueAt("", i, 1);

		currentTable.updateUI();
	}
}
